package com.koalac.ironman.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;

public class SMSUtil {

	private static Uri SMS_INBOX = Uri.parse("content://sms/");

	/**
	 * 获取短信验证码
	 * 
	 * @param activity
	 * @param mobileNum
	 * @return
	 */
	public static String getSmsyzm(Activity activity) {
		ContentResolver cr = activity.getContentResolver();
		String[] projection = new String[] { "body", "address", "person" };
		String where = " date >  "
				+ (System.currentTimeMillis() - 10 * 60 * 1000);
		Cursor cur = cr.query(SMS_INBOX, projection, where, null, "date desc");
		if (null == cur)
			return null;
		if (cur.moveToNext()) {
			String number = cur.getString(cur.getColumnIndex("address"));// 手机号
			String name = cur.getString(cur.getColumnIndex("person"));// 联系人姓名列表
			String body = cur.getString(cur.getColumnIndex("body"));

			/**
			 * 这里我是要获取自己短信服务号码中的验证码
			 */
			Pattern pattern = Pattern.compile("[a-zA-Z0-9]{6}");
			Matcher matcher = pattern.matcher(body);
			if (matcher.find()) {
				String res = matcher.group().substring(0, 6);// 获取短信的内容
				return res;
			}
		}
		return null;
	}

}
